package cn.huaguo.infrastructure.persistent.repository;

import cn.huaguo.domain.user.model.entity.UserEntity;
import cn.huaguo.domain.user.repository.IUserRepository;
import cn.huaguo.infrastructure.persistent.dao.IUserDao;
import cn.huaguo.infrastructure.persistent.dao.IWalletDao;
import cn.huaguo.infrastructure.persistent.po.User;
import cn.huaguo.infrastructure.persistent.po.Wallet;
import cn.huaguo.infrastructure.persistent.redis.IRedisService;
import cn.huaguo.types.common.Constants;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @Description 用户仓储接口实现类
 * @Author wangQian
 * @Date 2024/5/13
 */
@Repository
public class UserRepository implements IUserRepository {
    @Resource
    private IUserDao userDao;
    @Resource
    private IWalletDao walletDao;
    @Resource
    private IRedisService redisService;

    /**
     * 根据openid获取用户信息
     *
     * @param openid
     * @return
     */
    @Override
    public UserEntity queryUserByOpenid(String openid) {
        User user = userDao.queryUserByOpenid(openid);
        if (user == null) {
            return null;
        }
        // 缓存用户任务数量
        String cacheKey = Constants.RedisKey.USER_TASK_COUNT_KEY + user.getOpenid();
        redisService.setValue(cacheKey, user.getTaskCount(), 600000);

        return UserEntity.builder()
                .openid(user.getOpenid())
                .userCampus(user.getUserCampus())
                .build();
    }

    /**
     * 新增用户
     *
     * @param userEntity
     */
    @Override
    public void addUser(UserEntity userEntity) {
        User user = User.builder()
                .openid(userEntity.getOpenid())
                .createTime(LocalDateTime.now())
                .taskCount(Constants.USER_TASK_COUNT_NONE)
                .build();

        Wallet wallet = Wallet.builder()
                .userId(userEntity.getOpenid())
                .balance(BigDecimal.ZERO)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();

        userDao.addUser(user);
        walletDao.insert(wallet);

        // 缓存用户任务数量
        String cacheKey = Constants.RedisKey.USER_TASK_COUNT_KEY + user.getOpenid();
        redisService.setValue(cacheKey, user.getTaskCount(), 600000);
    }

    /**
     * 根据id获取用户信息
     *
     * @param openId
     * @return
     */
    @Override
    public UserEntity queryUserById(String openId) {
        User user = userDao.queryUserByOpenid(openId);
        UserEntity result = UserEntity.builder()
                .openid(user.getOpenid())
                .userName(user.getUserName())
                .userPhone(user.getUserPhone())
                .sex(user.getSex())
                .avatar(user.getAvatar())
                .userCampus(user.getUserCampus())
                .taskCount(user.getTaskCount())
                .build();
        return result;
    }

    /**
     * 获取用户接单数量
     *
     * @param openId
     * @return
     */
    @Override
    public String getUserTaskCount(String openId) {
        String cacheKey = Constants.RedisKey.USER_TASK_COUNT_KEY + openId;
        String taskCount = redisService.getValue(cacheKey);
        if (taskCount == null || ("").equals(taskCount)) {
            User user = userDao.queryUserByOpenid(openId);
            taskCount = user.getTaskCount();
        }

        // 缓存用户任务数量
        redisService.setValue(cacheKey, taskCount, 600000);
        return taskCount;
    }

    /**
     * 根据id更新用户任务数量
     *
     * @param openId
     * @param taskCount
     * @return
     */
    @Override
    public void updateUserTaskCount(String openId, String taskCount) {
        userDao.updateUserTaskCount(openId, taskCount);
        // 缓存用户任务数量
        String cacheKey = Constants.RedisKey.USER_TASK_COUNT_KEY + openId;
        redisService.setValue(cacheKey, taskCount, 600000);
    }
}
